<?php

namespace Johnm\Probackend\Controllers;

use Controller;
use Route;
use Redirect;
use Request;
use View;

class CoreController extends Controller {

    /**
     * Setup layout used by this controller
     * @var String
     */
    protected $layout = "probackend::layouts.main";

    /**
     * Setup actions need use layouts default only index action need use layouts
     * @var Array
     */
    protected $actionsNeedLayout = ['index'];

    /**
     * Setup actions need use layouts default only index action need use CRUD
     * @var Array
     */
    protected $actionsNeedCrub = ['index'];

    /**
     * Setup the layout used by the controller. If you want setup your layour 
     * please over protected $layout. And if you wan't to setup action use layout
     * please over protected $actionsNeedLayout
     * 
     *
     * @return void
     */
    protected function setupLayout() {
        $cAction = explode("@", Route::currentRouteAction());
        $cAction = $cAction[1];
        if (isset($cAction) && in_array($cAction, $this->actionsNeedLayout) && !is_null($this->layout)) {
            $this->layout = View::make($this->layout,["needCrub"=>in_array($cAction, $this->actionsNeedCrub)]);
        }
    }

    /**
     * Redirect back with input and with provided data
     *
     * @param Array $data all the data
     * @return void
     */
    protected function redirectBack($data = []) {
        return Redirect::back()->withInput()->with($data);
    }

    /**
     * Redirect to the previous url
     *
     * @return void
     */
    protected function redirectReferer() {
        $referer = $this->getRefer();
        return Redirect::to($referer);
    }
    
    protected function getRefer(){
        return Request::server('HTTP_REFERER');
    }
    
    

    /**
     * Redirect to a given route, with optional data
     *
     * @param String $route route name
     * @param Array $data optional data
     * @return void
     */
    protected function redirectRoute($route, $data = []) {
        return Redirect::route($route, $data);
    }

}
